package org.deng.learn.concurrent.basic;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author 邓力宾
 * @date 2020/5/15 下午4:16
 * @desc 屏障 同时起跑
 */
public class CyclicBarrierDemo {
    private final  static  int THREAD_NUM = 10; // 线程数
    private static CyclicBarrier cyclicBarrier = new CyclicBarrier(THREAD_NUM);

    public static void main(String[] args) {
        for(int i = 0; i< THREAD_NUM; i++){
            final int x = i;
            new Thread(()->{
                System.out.println(x + "准备");
                try {
                    cyclicBarrier.await(); // 当10个线程都准备好后才继续执行
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
                System.out.println(x + "开始");
            }).start();
        }
    }
}
